package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.ShoppingCartMapper;
import org.example.model.cart.ShoppingCart;
import org.example.model.common.dtos.cart.ShoppingCartDto;
import org.example.service.DishBasicService;
import org.example.service.SetmealBasicService;
import org.example.service.ShoppingCartService;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车
 */
@Service
@Slf4j
@Transactional
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishBasicService dishBasicService;

    @Autowired
    private SetmealBasicService setmealBasicService;

    /**
     * 新增购物车数据
     *
     * @param shoppingCart
     */
    @Override
    public Integer InsertShoppingCart(ShoppingCart shoppingCart) {
        if (shoppingCart == null) return 0;
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return 0;
        shoppingCart.setTotalMoney(shoppingCart.getTotalMoney() * shoppingCart.getNum());
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateDate(LocalDateTime.now());
        shoppingCart.setUpdateDate(LocalDateTime.now());
        shoppingCart.setIsDeleted(false);
        shoppingCartMapper.insert(shoppingCart);
        return shoppingCart.getId();
    }

    /**
     * 根据id获取购物车数据
     *
     * @param id
     */
    @Override
    public ShoppingCartDto SelectShoppingCartById(int id) {
        if (id <= 0) return new ShoppingCartDto();
        ShoppingCart shoppingCart = shoppingCartMapper.selectById(id);
        int dishId = shoppingCart.getDishId();
        int setmealId = shoppingCart.getSetmealId();
        ShoppingCartDto shoppingCartDto = new ShoppingCartDto();
        BeanUtils.copyProperties(shoppingCart, shoppingCartDto);
        if (dishId > 0) {
            shoppingCartDto.setDishBasicDto(dishBasicService.SelectDishById(dishId));
        }
        if (setmealId > 0) {
            shoppingCartDto.setSetmealBasicDto(setmealBasicService.SelectSetmealById(setmealId));
        }
        return shoppingCartDto;
    }

    /**
     * 通过token中的用户id获取对应的购物车列表
     */
    @Override
    public List<ShoppingCartDto> SelectShoppingCartsByUseId() {
        List<ShoppingCartDto> shoppingCartDtos = new ArrayList<>();
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return shoppingCartDtos;
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::isIsDeleted,false)
                .eq(ShoppingCart::getUserId,userId)
                .orderByDesc(ShoppingCart::getCreateDate);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
        ShoppingCartDto shoppingCartDto = null;
        for (ShoppingCart shoppingCart : shoppingCarts) {
            shoppingCartDto = this.SelectShoppingCartById(shoppingCart.getId());
            shoppingCartDtos.add(shoppingCartDto);
        }
        return shoppingCartDtos;
    }

    /**
     * 根据id修改对应购物车数据
     *
     * @param shoppingCart
     */
    @Override
    public Boolean UpdateShoppingCartById(ShoppingCart shoppingCart) {
        if (shoppingCart == null) return false;
        ShoppingCart cart = shoppingCartMapper.selectById(shoppingCart.getId());
        cart.setNum(shoppingCart.getNum());
        cart.setUpdateDate(LocalDateTime.now());
        cart.setIsDeleted(shoppingCart.isIsDeleted());
        return shoppingCartMapper.updateById(cart) > 0;
    }

    /**
     * 根据id列表批量删除购物车数据
     *
     * @param ids
     */
    @Override
    public Boolean DeleteShoppingCartByIds(int[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        for (int id : ids) {
            ShoppingCart cart = shoppingCartMapper.selectById(id);
            cart.setIsDeleted(true);
            Boolean flag = this.UpdateShoppingCartById(cart);
            if (!flag) return false;
        }
        return true;
    }
}
